package com.foodcommunity.controller;

import com.foodcommunity.entity.FoodPost;
import com.foodcommunity.entity.User;
import com.foodcommunity.service.FoodPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user/posts")
public class UserPostController {

    @Autowired
    private FoodPostService foodPostService;

    @GetMapping("")
    public String userPosts(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }

        List<FoodPost> posts = foodPostService.getUserPosts(user.getId());
        model.addAttribute("posts", posts);
        return "user/posts";
    }
} 